package com.amazonservices.mws.products.client;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GetFollowClient
{

	private static String divId = "olpOfferList";
	
	public static Map<String, Object> getUrlParams(String param) {  
	    Map<String, Object> map = new HashMap<String, Object>(0);  
	    if ((null == param) || ("".equalsIgnoreCase(param)) ) {  
	        return map;  
	    }  
	    String[] params = param.split("&");  
	    for (int i = 0; i < params.length; i++) {  
	        String[] p = params[i].split("=");  
	        if (p.length == 2) {  
	            map.put(p[0], p[1]);  
	        }  
	    }  
	    return map;  
	}  
	
	public static void main(String[] args)
	{
		String url = "http://www.amazon.com/gp/offer-listing/B01E7DAALE/ref=dp_olp_new_mbc?ie=UTF8&condition=new";
		
		try
		{
			Connection conn = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36")
												.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
												.header("Accept-Encoding", "gzip, deflate, sdch")
												.header("Accept-Language", "zh-CN,zh;q=0.8")
												.header("Avail-Dictionary", "TmgOVeVS")
												.header("Cache-Control", "max-age=0")
												.header("Connection", "keep-alive")
												.header("Host", "www.amazon.com")
												.header("Upgrade-Insecure-Requests", "1")
												.timeout(10000)
												;
			
			Document doc = conn.get();
			
			Element content = doc.getElementById(divId);
			
			Elements offers = content.getElementsByClass("olpOffer");
			
			for(Element ele : offers)
			{
				Elements priceElements = ele.getElementsByClass("olpOfferPrice");
				if(priceElements.size() > 0)
				{
					Element price = priceElements.get(0);
					System.out.println(price.html());
				}
				
				Elements shippingElements = ele.getElementsByClass("olpShippingPrice");
				if(shippingElements.size() > 0)
				{
					Element shippingPrice = shippingElements.get(0);
					String sp = shippingPrice.html();
					if((null == sp) || ("".equalsIgnoreCase(sp.trim())))
					{
						sp = "0";
					}
					System.out.println(sp);
				}
				
				Elements sellerElements = ele.getElementsByClass("olpSellerProfile");
				if(sellerElements.size() > 0)
				{
					Element seller = sellerElements.get(0).getElementsByTag("a").get(0);
					System.out.println(getUrlParams(seller.attr("href")).get("seller"));
				}
				
			}
			
			//System.out.println(content.html());
//			System.out.println("******************************************************************************************");
//			
//			Elements priceElements = doc.getElementsByClass("olpOfferPrice");
//			
//			for(Element ele : priceElements)
//			{
//				System.out.println(ele.html());
//			}
//			System.out.println("******************************************************************************************");
//			
//			Elements sellerElements = doc.getElementsByClass("olpSellerProfile");
//			
//			for(Element ele : sellerElements)
//			{
//				System.out.println(ele.html());
//			}
			
			
		} catch (IOException e)
		{
			e.printStackTrace();
		}

	}

}
